from pythonds.basic.stack import Stack


def postfixEval(postfixExpr):
    operandStark = Stack()  # 暂存操作数
    tokenList = postfixExpr.split()  # 解析为单词列表
    print(tokenList)
    for token in tokenList:
        if token in "0123456789":
            operandStark.push(token)
        else:
            operand2 = operandStark.pop()  # 右操作数
            operand1 = operandStark.pop()  # 左操作数
            result = doMath(token, operand1, operand2)
            operandStark.push(result)
    return operandStark.pop()


def doMath(op, op1, op2):
    op1 = int(op1)
    op2 = int(op2)
    if op == "*":
        return op1 * op2
    elif op == "/":
        return op1 / op2
    elif op == "+":
        return op1 + op2
    else:
        return op1 - op2


print(postfixEval("7 8 + 3 2 + /"))
print(postfixEval("4 5 6 * +"))
